/**
	Copyright (c) 2010 ATTi 
	All rights reserved.
	Author: Michael Shu
	Date: 1/26/2010
*/
@istest

private class EventWithOpportunityTriggerTester {
	public static testMethod void testEvent() {
		
		RecordTypeHelper.PrepareToLoad('Event');
		RecordTypeHelper.PrepareToLoad('Account');
		RecordTypeHelper.PrepareToLoad('Opportunity');
		
		Event e = new Event();
		e.RecordTypeId = RecordTypeHelper.GetRecordTypeID('Event', 'Local Advertiser Event');
		e.DurationInMinutes = 10;		
		e.ActivityDateTime = Datetime.now();
		e.Type = 'Appointment';
		e.StartDateTime = e.ActivityDateTime;
		List<Event> events = new List<Event>();
		events.add(e);
		Database.SaveResult[] event_rsts = Database.insert(events);	
		
		Event e_udpate = [select Id, WhatId from Event where Id =: event_rsts[0].getId()];
		
		List<Account> accs = new List<Account>();
		Account a = new Account();
		a.RecordTypeId = RecordTypeHelper.GetRecordTypeID('Account', 'Local OOR Customer');
		a.Name = 'EventWithOpportunityTriggerTester_Account';
		accs.add(a);
		Database.SaveResult[] acc_rsts = Database.insert(accs);
		
		List<Opportunity> opps = new List<Opportunity>();
		Opportunity o = new Opportunity();
		o.Name = 'EventWithOpportunityTriggerTester';
		o.StageName = 'Open';
		o.CloseDate = Date.today();
		o.AccountId = acc_rsts[0].Id;
		o.RecordTypeId = RecordTypeHelper.GetRecordTypeID('Opportunity', 'Local Advertiser');
        opps.add(o);
        Database.SaveResult[] opp_rsts = Database.insert(opps);	
		
		List<Event> events_new = new List<Event>();
        Event e1 = new Event();
		e1.DurationInMinutes = 10;	
		e1.RecordTypeId = RecordTypeHelper.GetRecordTypeID('Event', 'Local Advertiser Event');	
		e1.ActivityDateTime = Datetime.now();
		e1.Type = 'Appointment';
		e1.StartDateTime = e1.ActivityDateTime;
		e1.WhatId = opp_rsts[0].getId();
		events_new.add(e1);
		
        Event e2 = new Event();
		e2.DurationInMinutes = 10;	
		e2.RecordTypeId = RecordTypeHelper.GetRecordTypeID('Event', 'Local Advertiser Event');	
		e2.ActivityDateTime = Datetime.now();
		e2.Type = 'Appointment';
		e2.StartDateTime = e2.ActivityDateTime;
		e2.WhatId = opp_rsts[0].getId();
		events_new.add(e2);
				
		Database.SaveResult[] event_rsts_new = Database.insert(events_new);	
		
		Event e_udpate_new = [select Id, WhatId from Event where Id =: event_rsts_new[0].getId()];
		delete e_udpate_new;
				
		e_udpate.WhatId = opp_rsts[0].getId();
		e_udpate.Type = 'Presentation';
		events = new List<Event>();
		events.add(e_udpate);
		event_rsts = Database.update(events);
		
		Event e_udpate_new_rst = [select Id, WhatId from Event where Id =: event_rsts[0].getId()];
		e_udpate_new_rst.Type = 'Presentation';
		events = new List<Event>();
		events.add(e_udpate_new_rst);
		event_rsts = Database.update(events);
		
		Event e_udpate_del = [select Id, WhatId from Event where Id =: event_rsts[0].getId()];
		delete e_udpate_del;			 
	}
	
}